package com.cgsd.heyu.ui.info;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.utils.BeanUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.AbonusTeamAdapter;
import com.cgsd.heyu.adapter.MyTeamAdapter;
import com.cgsd.heyu.entity.AbonusTeamEntity;
import com.cgsd.heyu.entity.TeamNumberEntity;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.widgets.ErrorCustomView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 区域人数
 * CreateData: 2021/1/7
 */
public class AbonusTeamActivity extends BaseActivity<HomeContract.Presenter> implements HomeContract.AbonusTeamView, OnLoadMoreListener {
    private AbonusTeamAdapter adapter;
    private List<AbonusTeamEntity> data = new ArrayList<AbonusTeamEntity>();
    private SmartRefreshLayout mSmartRefreshLayout;
    private int pageSize = 20;
    private ErrorCustomView errorCustomView;

    private TextView mTvNum1;
    private TextView mTvNum2;
    private TextView mTvNum3;
    public static void actionStart(Activity activity){
        Intent intent = new Intent(activity, AbonusTeamActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_abonus_team;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);

        mTvNum1 = findViewById(R.id.tv_num1);
        mTvNum2 = findViewById(R.id.tv_num2);
        mTvNum3 = findViewById(R.id.tv_num3);

        mSmartRefreshLayout = findViewById(R.id.mSmartRefreshLayout);
        errorCustomView = findViewById(R.id.mErrorView);
        mSmartRefreshLayout.setEnableRefresh(false);

        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AbonusTeamAdapter(this,data);
        mRecyclerView.setAdapter(adapter);

        mSmartRefreshLayout.setOnLoadMoreListener(this);

    }

    @Override
    protected void initData() {
        mPresenter.getAreaTeamData();
        getList();
    }

    @Override
    public void setAreaTeamData(TeamNumberEntity entity) {
        if (entity != null){
            mTvNum1.setText(entity.areaNum);
            mTvNum2.setText(entity.areaVipNum);
            mTvNum3.setText(entity.areaAgentNum);
        }
    }

    @Override
    public void abonusTeamList(List<AbonusTeamEntity> entity) {
        dismissCommonSubmitDialog();
        mSmartRefreshLayout.finishLoadMore();
        if (BeanUtils.isNotEmpty(entity)){
            int index = data.size();
            data.addAll(entity);
           // adapter.notifyItemRangeChanged(index,entity.size());

            if (entity.size() < pageSize) {
                mSmartRefreshLayout.setEnableLoadMore(false);
            } else {
                mSmartRefreshLayout.setEnableLoadMore(true);
            }
        }else {
            mSmartRefreshLayout.setEnableLoadMore(false);
        }
        if (data.size() == 0){
            showErrorView();
        }else {
            errorCustomView.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        getList();
    }

    /**
     * 数据为null
     */
    private void showErrorView(){
        errorCustomView.showNetworkError(R.mipmap.default_no_data,"暂无会员～");
        errorCustomView.setVisibility(View.VISIBLE);
    }

    private void getList(){
        showCommonSubmitDialog("");
        mPresenter.abonusTeamList(page, pageSize);
    }


}
